package ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import service.Impl.IBookServiceImpl;
import service.Impl.ICartLineServiceImpl;
import service.Impl.IOrderServiceImpl;
import vo.*;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/*
 * @description: 从购物车页面中提交订单
 * 从前端接收：cartLineId  地址  从session获得：userId
 * 传给前端：
 */
public class OrderCommitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
//        ServletInputStream inputStream=req.getInputStream();
//        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
//        StringBuilder stringBuilder=new StringBuilder();
//        String temp="";
//        while((temp=bufferedReader.readLine())!=null){
//            stringBuilder.append(temp);
//        }
//        String jsonString=stringBuilder.toString();
//        JSONObject jsonObject=JSON.parseObject(jsonString);
//
////        DeliveryAddress deliveryAddress= (DeliveryAddress) jsonObject.get("deliveryAddress");
//
//        JSONArray jsonArray=jsonObject.getJSONArray("cartLines");
//
//        //根据前端信息获取到cartLine集合
//        ICartLineServiceImpl cartLineService=new ICartLineServiceImpl();
//        List<CartLine>cartLines=new ArrayList<>();

//        if(jsonArray.size()>0){
//            for(int i=0;i<jsonArray.size();i++){
//                JSONObject object=jsonArray.getJSONObject(i);
//                int cartLineId=Integer.parseInt(object.getString("id"));
//                CartLine cartLine=cartLineService.selectCartLineById(cartLineId);
//                cartLines.add(cartLine);
//            }
//        }

//        //测试
//        int id1=Integer.parseInt(req.getParameter("cartLine1"));
//        int id2=Integer.parseInt(req.getParameter("cartLine2"));
//
//        ICartLineServiceImpl cartLineService=new ICartLineServiceImpl();
//        List<CartLine>cartLines=new ArrayList<>();
//        CartLine cartLine1=cartLineService.selectCartLineById(id1);
//        cartLines.add(cartLine1);
//        CartLine cartLine2=cartLineService.selectCartLineById(id2);
//        cartLines.add(cartLine2);


        //从当前会话中获取用户信息
        HttpSession httpSession=req.getSession();
        List<CartLine>cartLines=(List<CartLine>) httpSession.getAttribute("cartLines");
        Client client= (Client) httpSession.getAttribute("client");
        if(client!=null){
            int userId=client.getId();

            //具体的业务逻辑


            IOrderServiceImpl orderService=new IOrderServiceImpl();
            List<OrderLine>orderLines=orderService.addOrder(cartLines,userId);
            double totalMoney=orderService.totalPrice(orderLines);
            int orderId=orderLines.get(0).getOrderId();
            httpSession.setAttribute("orderId",orderId);

//            IBookServiceImpl bookService=new IBookServiceImpl();
//            JSONObject returnJson=new JSONObject();
//            returnJson.put("totalPrice",totalMoney);
//            JSONArray orderLineArray=new JSONArray();
//            for(OrderLine orderLine:orderLines){
//                JSONObject object=new JSONObject();
//                object.put("id",orderLine.getId());
//                object.put("orderId",orderLine.getOrderId());
//                object.put("bookId",orderLine.getBookId());
//                object.put("quanlity",orderLine.getQuanlity());
//                Book book=bookService.selectBookById(orderLine.getBookId());
//                object.put("name",book.getName());
//                object.put("price",book.getPrice());
//                object.put("type",book.getType());
//                object.put("imgUrl",book.getImgUrl());
//                orderLineArray.add(object);
//            }
//            returnJson.put("orderLines",orderLineArray);


            //resp.getWriter().write(returnJson.toString());
            resp.sendRedirect("/cart.jsp");
        }else{
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("{\"status\":102}");
        }

    }
}
